home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rcs55.zip / CI.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  35KB  |  1,082 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Copyright 1990 by Paul Eggert
  3.    Distributed under license by the Free Software Foundation, Inc.
  4.  
  5. This file is part of RCS.
  6.  
  7. RCS is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. RCS is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with RCS; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. Report problems and direct all questions to:
  22.  
  23.     rcs-bugs@cs.purdue.edu
  24.  
  25. */
  26.  
  27. /*
  28.  *                     RCS checkin operation
  29.  */
  30. /*******************************************************************
  31.  *                       check revisions into RCS files
  32.  *******************************************************************
  33.  */
  34.  
  35.  
  36.  
  37. /* $Log: ci.c%v $
  38.  * Revision 1.2  1991/08/20  07:11:48  SGP
  39.  * Ported to MSDOS using Borland C++
  40.  *
  41.  * Revision 5.12  1990/12/31  01:00:12  eggert
  42.  * Don't use uninitialized storage when handling -{N,n}.
  43.  *
  44.  * Revision 5.11  1990/12/04  05:18:36  eggert
  45.  * Use -I for prompts and -q for diagnostics.
  46.  *
  47.  * Revision 5.10  1990/11/05  20:30:10  eggert
  48.  * Don't remove working file when aborting due to no changes.
  49.  *
  50.  * Revision 5.9  1990/11/01  05:03:23  eggert
  51.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  52.  *
  53.  * Revision 5.8  1990/10/04  06:30:09  eggert
  54.  * Accumulate exit status across files.
  55.  *
  56.  * Revision 5.7  1990/09/25  20:11:46  hammer
  57.  * fixed another small typo
  58.  *
  59.  * Revision 5.6  1990/09/24  21:48:50  hammer
  60.  * added cleanups from Paul Eggert.
  61.  *
  62.  * Revision 5.5  1990/09/21  06:16:38  hammer
  63.  * made it handle multiple -{N,n}'s.  Also, made it treat re-directed stdin
  64.  * the same as the terminal
  65.  *
  66.  * Revision 5.4  1990/09/20  02:38:51  eggert
  67.  * ci -k now checks dates more thoroughly.
  68.  *
  69.  * Revision 5.3  1990/09/11  02:41:07  eggert
  70.  * Fix revision bug with `ci -k file1 file2'.
  71.  *
  72.  * Revision 5.2  1990/09/04  08:02:10  eggert
  73.  * Permit adjacent revisions with identical time stamps (possible on fast hosts).
  74.  * Improve incomplete line handling.  Standardize yes-or-no procedure.
  75.  *
  76.  * Revision 5.1  1990/08/29  07:13:44  eggert
  77.  * Expand locker value like co.  Clean old log messages too.
  78.  *
  79.  * Revision 5.0  1990/08/22  08:10:00  eggert
  80.  * Don't require a final newline.
  81.  * Make lock and temp files faster and safer.
  82.  * Remove compile-time limits; use malloc instead.
  83.  * Permit dates past 1999/12/31.  Switch to GMT.
  84.  * Add setuid support.  Don't pass +args to diff.  Check diff's output.
  85.  * Ansify and Posixate.  Add -k, -V.  Remove snooping.  Tune.
  86.  * Check diff's output.
  87.  *
  88.  * Revision 4.9  89/05/01  15:10:54  narten
  89.  * changed copyright header to reflect current distribution rules
  90.  * 
  91.  * Revision 4.8  88/11/08  13:38:23  narten
  92.  * changes from root@seismo.CSS.GOV (Super User)
  93.  * -d with no arguments uses the mod time of the file it is checking in
  94.  * 
  95.  * Revision 4.7  88/08/09  19:12:07  eggert
  96.  * Make sure workfile is a regular file; use its mode if RCSfile doesn't have one.
  97.  * Use execv(), not system(); allow cc -R; remove lint.
  98.  * isatty(fileno(stdin)) -> ttystdin()
  99.  * 
  100.  * Revision 4.6  87/12/18  11:34:41  narten
  101.  * lint cleanups (from Guy Harris)
  102.  * 
  103.  * Revision 4.5  87/10/18  10:18:48  narten
  104.  * Updating version numbers. Changes relative to revision 1.1 are actually
  105.  * relative to 4.3
  106.  * 
  107.  * Revision 1.3  87/09/24  13:57:19  narten
  108.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  109.  * warnings)
  110.  * 
  111.  * Revision 1.2  87/03/27  14:21:33  jenkins
  112.  * Port to suns
  113.  * 
  114.  * Revision 4.3  83/12/15  12:28:54  wft
  115.  * ci -u and ci -l now set mode of working file properly.
  116.  * 
  117.  * Revision 4.2  83/12/05  13:40:54  wft
  118.  * Merged with 3.9.1.1: added calls to clearerr(stdin).
  119.  * made rewriteflag external.
  120.  * 
  121.  * Revision 4.1  83/05/10  17:03:06  wft
  122.  * Added option -d and -w, and updated assingment of date, etc. to new delta.
  123.  * Added handling of default branches.
  124.  * Option -k generates std. log message; fixed undef. pointer in reading of log.
  125.  * Replaced getlock() with findlock(), link--unlink with rename(),
  126.  * getpwuid() with getcaller().
  127.  * Moved all revision number generation to new routine addelta().
  128.  * Removed calls to stat(); now done by pairfilenames().
  129.  * Changed most calls to catchints() with restoreints().
  130.  * Directed all interactive messages to stderr.
  131.  * 
  132.  * Revision 3.9.1.1  83/10/19  04:21:03  lepreau
  133.  * Added clearerr(stdin) to getlogmsg() for re-reading stdin.
  134.  * 
  135.  * Revision 3.9  83/02/15  15:25:44  wft
  136.  * 4.2 prerelease
  137.  * 
  138.  * Revision 3.9  83/02/15  15:25:44  wft
  139.  * Added call to fastcopy() to copy remainder of RCS file.
  140.  *
  141.  * Revision 3.8  83/01/14  15:34:05  wft
  142.  * Added ignoring of interrupts while new RCS file is renamed;
  143.  * Avoids deletion of RCS files by interrupts.
  144.  *
  145.  * Revision 3.7  82/12/10  16:09:20  wft
  146.  * Corrected checking of return code from diff.
  147.  *
  148.  * Revision 3.6  82/12/08  21:34:49  wft
  149.  * Using DATEFORM to prepare date of checked-in revision;
  150.  * Fixed return from addbranch().
  151.  *
  152.  * Revision 3.5  82/12/04  18:32:42  wft
  153.  * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE. Updated
  154.  * field lockedby in removelock(), moved getlogmsg() before calling diff.
  155.  *
  156.  * Revision 3.4  82/12/02  13:27:13  wft
  157.  * added option -k.
  158.  *
  159.  * Revision 3.3  82/11/28  20:53:31  wft
  160.  * Added mustcheckin() to check for redundant checkins.
  161.  * Added xpandfile() to do keyword expansion for -u and -l;
  162.  * -m appends linefeed to log message if necessary.
  163.  * getlogmsg() suppresses prompt if stdin is not a terminal.
  164.  * Replaced keeplock with lockflag, fclose() with ffclose(),
  165.  * %02d with %.2d, getlogin() with getpwuid().
  166.  *
  167.  * Revision 3.2  82/10/18  20:57:23  wft
  168.  * An RCS file inherits its mode during the first ci from the working file,
  169.  * otherwise it stays the same, except that write permission is removed.
  170.  * Fixed ci -l, added ci -u (both do an implicit co after the ci).
  171.  * Fixed call to getlogin(), added call to getfullRCSname(), added check
  172.  * for write error.
  173.  * Changed conflicting identifiers.
  174.  *
  175.  * Revision 3.1  82/10/13  16:04:59  wft
  176.  * fixed type of variables receiving from getc() (char -> int).
  177.  * added include file dbm.h for getting BYTESIZ. This is used
  178.  * to check the return code from diff portably.
  179.  */
  180.  
  181. #include "rcsbase.h"
  182.  
  183. struct Symrev {
  184.        const char *ssymbol;
  185.        int override;
  186.        struct Symrev * nextsym;
  187. };
  188.  
  189. /* rcsfcmp */
  190. int rcsfcmp P((const char*,const char*,const struct hshentry*));
  191.  
  192. /* rcskeep */
  193. extern char prevdate[];
  194. extern struct buf prevauthor, prevrev, prevstate;
  195. int getoldkeys P((FILE*));
  196.  
  197. static const char *xpandfile P((const char*,const char*,const struct hshentry*));
  198. static const char *getdate P((void));
  199. static int addbranch P((struct hshentry*,struct buf*));
  200. static int addelta P((void));
  201. static int mustcheckin P((const char*,const struct hshentry*));
  202. static struct cbuf getlogmsg P((void));
  203. static struct hshentry *removelock P((struct hshentry*));
  204. static void cleanup P((void));
  205. static void incnum P((const char*,struct buf*));
  206. static void addassoclst P((int, char *));
  207.  
  208. static const char diff[] = DIFF;
  209.  
  210. static FILE *workptr;            /* working file pointer        */
  211. static const char *olddeltanum;        /* number of old delta        */
  212. static struct buf newdelnum;        /* new revision number        */
  213. static struct cbuf msg;
  214. static int exitstatus;
  215. static int forceciflag;            /* forces check in        */
  216. static int keepflag, keepworkingfile, rcsinitflag;
  217. static struct hshentries *gendeltas;    /* deltas to be generated    */
  218. static struct hshentry *targetdelta;    /* old delta to be generated    */
  219. static struct hshentry newdelta;    /* new delta to be inserted    */
  220. static struct Symrev *assoclst, *lastassoc;
  221.  
  222. mainProg(ciId, "ci", "$Id: ci.c%v 1.2 1991/08/20 07:11:48 SGP Exp $")
  223. {
  224.     static const char cmdusage[] =
  225.         "\nci usage: ci -{fklqru}[rev] -mmsg -{nN}name -sstate -t[textfile] -Vn file ...";
  226.  
  227.     char altdate[datesize];
  228.     const char *author, *krev, *rev, *state, *textfile;
  229.     const char *diffilename, *expfilename;
  230.     const char *workdiffname, *newworkfilename;
  231.     int exit_stats;         /* return code for command invocations     */
  232.     int lockflag;
  233.     int r;
  234.     int usestatdate; /* Use mod time of file for -d.  */
  235.     mode_t newRCSmode; /* mode for RCS file */
  236.     mode_t newworkmode; /* mode for working file */
  237.     struct Symrev *curassoc;
  238.     
  239.     initid();
  240.     catchints();
  241.  
  242.     author = rev = state = textfile = nil;
  243.     curassoc = assoclst = lastassoc = (struct Symrev *) nil;
  244.     lockflag = false;
  245.     altdate[0]= '\0'; /* empty alternate date for -d */
  246.     usestatdate=false;
  247.  
  248.         while (--argc,++argv, argc>=1 && ((*argv)[0] == '-')) {
  249.                 switch ((*argv)[1]) {
  250.  
  251.                 case 'r':
  252.             keepworkingfile = lockflag = false;
  253.                 revno:  if ((*argv)[2]!='\0') {
  254.                 if (rev) warn("redefinition of revision number");
  255.                                 rev = (*argv)+2;
  256.                         }
  257.                         break;
  258.  
  259.                 case 'l':
  260.                         keepworkingfile=lockflag=true;
  261.                         goto revno;
  262.  
  263.                 case 'u':
  264.                         keepworkingfile=true; lockflag=false;
  265.                         goto revno;
  266.  
  267.         case 'I':
  268.             interactiveflag = true;
  269.             goto revno;
  270.  
  271.                 case 'q':
  272.                         quietflag=true;
  273.                         goto revno;
  274.  
  275.                 case 'f':
  276.                         forceciflag=true;
  277.                         goto revno;
  278.  
  279.                 case 'k':
  280.                         keepflag=true;
  281.                         goto revno;
  282.  
  283.                 case 'm':
  284.             if (msg.size) redefined('m');
  285.             msg = cleanlogmsg(*argv+2, strlen(*argv+2));
  286.             if (!msg.size)
  287.                 warn("missing message for -m option");
  288.                         break;
  289.  
  290.                 case 'n':
  291.             if ((*argv)[2] == '\0') {
  292.                                 error("missing symbolic name after -n");
  293.                 break;
  294.                     }
  295.                    checksid((*argv)+2);
  296.                     addassoclst(false, (*argv)+2);
  297.                 break;
  298.         
  299.         case 'N':
  300.             if ((*argv)[2] == '\0') {
  301.                                 error("missing symbolic name after -N");
  302.                 break;
  303.                     }
  304.                     checksid((*argv)+2);
  305.                     addassoclst(true, (*argv)+2);
  306.                 break;
  307.  
  308.                 case 's':
  309.                         if ((*argv)[2]!='\0'){
  310.                 if (state) redefined('s');
  311.                 checksid((*argv)+2);
  312.                                 state = (*argv)+2;
  313.             } else
  314.                 warn("missing state for -s option");
  315.                         break;
  316.  
  317.                 case 't':
  318.                         if ((*argv)[2]!='\0'){
  319.                 if (textfile) redefined('t');
  320.                                 textfile = (*argv)+2;
  321.                         }
  322.                         break;
  323.  
  324.         case 'd':
  325.             if (altdate[0] || usestatdate)
  326.                 redefined('d');
  327.             altdate[0] = 0;
  328.             usestatdate = false;
  329.             if ((*argv)[2])
  330.                 str2date(*argv+2, altdate);
  331.             else
  332.                 usestatdate = true;
  333.                         break;
  334.  
  335.         case 'w':
  336.                         if ((*argv)[2]!='\0'){
  337.                 if (author) redefined('w');
  338.                 checksid((*argv)+2);
  339.                 author = (*argv)+2;
  340.             } else
  341.                 warn("missing author for -w option");
  342.                         break;
  343.  
  344.         case 'V':
  345.             setRCSversion(*argv);
  346.             break;
  347.  
  348.  
  349.  
  350.                 default:
  351.             faterror("unknown option: %s%s", *argv, cmdusage);
  352.                 };
  353.         }  /* end processing of options */
  354.  
  355.     if (argc<1) faterror("no input file%s", cmdusage);
  356.  
  357.         /* now handle all filenames */
  358.         do {
  359.         finptr=frewrite=NULL;
  360.     fcopy = foutptr = NULL;
  361.     workptr = NULL;
  362.         targetdelta=nil;
  363.         olddeltanum=nil;
  364.     ffree();
  365.  
  366.     switch (pairfilenames(argc, argv, rcswriteopen, false, false)) {
  367.  
  368.         case -1:                /* New RCS file */
  369.         rcsinitflag = true;
  370.                 break;
  371.  
  372.         case 0:                 /* Error */
  373.                 continue;
  374.  
  375.         case 1:                 /* Normal checkin with prev . RCS file */
  376.         rcsinitflag = !Head;
  377.         }
  378.  
  379.         /* now RCSfilename contains the name of the RCS file, and
  380.          * workfilename contains the name of the working file.
  381.      * If the RCS file exists, finptr contains the file descriptor for the
  382.          * RCS file. The admin node is initialized.
  383.      * RCSstat is set.
  384.          */
  385.  
  386.     diagnose("%s  <--  %s\n", RCSfilename,workfilename);
  387.  
  388.     errno = 0;
  389.     if (!(workptr = fopen(workfilename,"r"))) {
  390.         eerror(workfilename);
  391.         continue;
  392.     }
  393.     if (!getfworkstat(fileno(workptr))) continue;
  394.     newRCSmode =
  395.           (rcsinitflag ? workstat.st_mode : RCSstat.st_mode)
  396.         & ~(S_IWUSR|S_IWGRP|S_IWOTH);
  397.     /* newRCSmode also adjusts mode of working file for -u and -l. */
  398.     if (finptr && !checkaccesslist()) continue; /* give up */
  399.  
  400.     krev = rev;
  401.         if (keepflag) {
  402.                 /* get keyword values from working file */
  403.         if (!getoldkeys(workptr)) continue;
  404.         if (!rev  &&  !*(krev = prevrev.string)) {
  405.             error("can't find a revision number in %s",workfilename);
  406.                         continue;
  407.                 }
  408.         if (*prevdate=='\0' && *altdate=='\0' && usestatdate==false)
  409.             warn("can't find a date in %s", workfilename);
  410.         if (!*prevauthor.string && !author)
  411.             warn("can't find an author in %s", workfilename);
  412.         if (!*prevstate.string && !state)
  413.             warn("can't find a state in %s", workfilename);
  414.         } /* end processing keepflag */
  415.  
  416.         gettree(); /* reads in the delta tree.*/
  417.  
  418.         /* expand symbolic revision number */
  419.     if (!expandsym(krev,&newdelnum)) continue;
  420.  
  421.         /* splice new delta into tree */
  422.         if (!addelta()) continue;
  423.  
  424.     if (rcsinitflag) {
  425.         diagnose("initial revision: %s\n", newdelnum.string);
  426.     } else  diagnose("new revision: %s; previous revision: %s\n",
  427.              newdelnum.string, olddeltanum);
  428.  
  429.     newdelta.num = newdelnum.string;
  430.         newdelta.branches=nil;
  431.         newdelta.lockedby=nil; /*might be changed by addlock() */
  432.     newdelta.selector = true;
  433.     /* set author */
  434.     if (author!=nil)
  435.         newdelta.author=author;     /* set author given by -w         */
  436.     else if (keepflag && *prevauthor.string)
  437.         newdelta.author=prevauthor.string; /* preserve old author if possible*/
  438.     else    newdelta.author=getcaller();/* otherwise use caller's id      */
  439.     if (state!=nil)
  440.         newdelta.state=state;       /* set state given by -s          */
  441.     else if (keepflag && *prevstate.string)
  442.         newdelta.state=prevstate.string;   /* preserve old state if possible */
  443.     else    newdelta.state=DEFAULTSTATE;/* otherwise use default state    */
  444.     if (usestatdate) {
  445.         time2date(workstat.st_mtime, altdate);
  446.     }
  447.     if (*altdate!='\0')
  448.         newdelta.date=altdate;      /* set date given by -d           */
  449.     else if (keepflag && *prevdate) /* preserve old date if possible  */
  450.         newdelta.date = prevdate;
  451.     else
  452.         newdelta.date = getdate();  /* use current date               */
  453.     /* now check validity of date -- needed because of -d and -k          */
  454.     if (targetdelta!=nil &&
  455.         cmpnum(newdelta.date,targetdelta->date) < 0) {
  456.         error("Date %s precedes %s in existing revision %s.",
  457.                newdelta.date,targetdelta->date, targetdelta->num);
  458.         continue;
  459.     }
  460.  
  461.  
  462.     if (lockflag  &&  addlock(&newdelta) < 0) continue;
  463.         curassoc = assoclst;
  464.     while (curassoc) {
  465.             if (!addsymbol(newdelta.num, curassoc->ssymbol, curassoc->override))
  466.                 break;
  467.             curassoc = curassoc->nextsym;
  468.     }
  469.     if (curassoc) continue;
  470.  
  471.     
  472.         putadmin(frewrite);
  473.         puttree(Head,frewrite);
  474.     putdesc(false,textfile);
  475.  
  476.  
  477.         /* build rest of file */
  478.     if (rcsinitflag) {
  479.                 /* get logmessage */
  480.                 newdelta.log=getlogmsg();
  481.         if (!putdftext(newdelnum.string,newdelta.log,workptr,frewrite,false)) continue;
  482.         } else {
  483.         diffilename = maketemp(0);
  484.         workdiffname = workfilename;
  485.         if (workdiffname[0] == '+') {
  486.             /* Some diffs have options with leading '+'. */
  487.             char *w = ftnalloc(char, strlen(workfilename)+3);
  488.             workdiffname = w;
  489.             *w++ = '.';
  490.             *w++ = SLASH;
  491.             VOID strcpy(w, workfilename);
  492.         }
  493.                 if (&newdelta==Head) {
  494.                         /* prepend new one */
  495.             foutptr = NULL;
  496.                         if (!(expfilename=
  497.                   buildrevision(gendeltas,targetdelta,false,false))) continue;
  498.                         if (!mustcheckin(expfilename,targetdelta)) continue;
  499.                                 /* don't check in files that aren't different, unless forced*/
  500.                         newdelta.log=getlogmsg();
  501.                         exit_stats = run((char*)nil,diffilename,
  502.                 diff DIFF_FLAGS, workdiffname, expfilename,
  503.                 (char*)nil);
  504.             if (!WIFEXITED(exit_stats) || 1<WEXITSTATUS(exit_stats))
  505.                             faterror ("diff failed");
  506.             /* diff status is EXIT_TROUBLE on failure. */
  507.             if (!putdftext(newdelnum.string,newdelta.log,workptr,frewrite,false)) continue;
  508.             if (!putdtext(olddeltanum,targetdelta->log,diffilename,frewrite,true)) continue;
  509.                 } else {
  510.                         /* insert new delta text */
  511.             foutptr = frewrite;
  512.                         if (!(expfilename=
  513.                   buildrevision(gendeltas,targetdelta,false,false))) continue;
  514.                         if (!mustcheckin(expfilename,targetdelta)) continue;
  515.                                 /* don't check in files that aren't different, unless forced*/
  516.                         newdelta.log=getlogmsg();
  517.                         exit_stats = run((char*)nil, diffilename,
  518.                 diff DIFF_FLAGS, expfilename, workdiffname,
  519.                 (char*)nil);
  520.             if (!WIFEXITED(exit_stats) || 1<WEXITSTATUS(exit_stats))
  521.                             faterror ("diff failed");
  522.             if (!putdtext(newdelnum.string,newdelta.log,diffilename,frewrite,true)) continue;
  523.                 }
  524.  
  525.                 /* rewrite rest of RCS file */
  526.                 fastcopy(finptr,frewrite);
  527.         ffclose(finptr); finptr=NULL; /* Help the file system. */
  528.         }
  529.     ffclose(frewrite); frewrite=NULL;
  530.     ffclose(workptr); workptr=NULL;
  531.     seteid();
  532.     if ((r = chmod(newRCSfilename,newRCSmode)) == 0) {
  533.         ignoreints();
  534.         r = re_name(newRCSfilename,RCSfilename);
  535.         keepdirtemp(newRCSfilename);
  536.         restoreints();
  537.     }
  538.     setrid();
  539.     if (r != 0) {
  540.         eerror(RCSfilename);
  541.         error("saved in %s", newRCSfilename);
  542.         dirtempunlink();
  543.                 break;
  544.         }
  545.  
  546.         if (!keepworkingfile) {
  547.         r = unlink(workfilename); /* Get rid of old file */
  548.         } else {
  549.         newworkmode = WORKMODE(newRCSmode,
  550.             !(Expand == OLD_EXPAND  ||  !lockflag && StrictLocks)
  551.         );
  552.         /* Expand if !OLD_EXPAND, or if mode can't be fixed.  */
  553.         if (
  554.             Expand != OLD_EXPAND
  555.         ||    (    workstat.st_mode != newworkmode
  556.             &&    (r = chmod(workfilename,newworkmode)) < 0
  557.             )
  558.         ) {
  559.             /* Expand keywords in file.  */
  560.             locker_expansion = lockflag;
  561.             newworkfilename=
  562.             xpandfile(workfilename,workfilename /*for directory*/,&newdelta);
  563.             if (!newworkfilename) continue; /* expand failed */
  564.             if ((r = chmod(newworkfilename, newworkmode)) == 0) {
  565.             ignoreints();
  566.             r = re_name(newworkfilename,workfilename);
  567.             keepdirtemp(newworkfilename);
  568.             restoreints();
  569.             }
  570.         }
  571.         }
  572.     if (r != 0) {
  573.         eerror(workfilename);
  574.         continue;
  575.     }
  576.     diagnose("done\n");
  577.  
  578.         } while (cleanup(),
  579.                  ++argv, --argc >=1);
  580.  
  581.     tempunlink();
  582.     exitmain(exitstatus);
  583. }       /* end of main (ci) */
  584.  
  585.     static void
  586. cleanup()
  587. {
  588.     if (nerror) exitstatus = EXIT_FAILURE;
  589.     if (finptr) ffclose(finptr);
  590.     if (frewrite) ffclose(frewrite);
  591.     if (workptr) ffclose(workptr);
  592.     dirtempunlink();
  593. }
  594.  
  595. #if lint
  596. #    define exiterr ciExit
  597. #endif
  598.     exiting void
  599. exiterr()
  600. {
  601.     dirtempunlink();
  602.     tempunlink();
  603.     _exit(EXIT_FAILURE);
  604. }
  605.  
  606. /*****************************************************************/
  607. /* the rest are auxiliary routines                               */
  608.  
  609.  
  610.     static int
  611. addelta()
  612. /* Function: Appends a delta to the delta tree, whose number is
  613.  * given by newdelnum.  Updates Head, newdelnum, newdelnumlength,
  614.  * olddeltanum and the links in newdelta.
  615.  * Returns false on error, true on success.
  616.  */
  617. {
  618.     register char *tp;
  619.     register unsigned i;
  620.     unsigned newdnumlength;  /* actual length of new rev. num. */
  621.  
  622.     newdnumlength = countnumflds(newdelnum.string);
  623.  
  624.     if (rcsinitflag) {
  625.                 /* this covers non-existing RCS file and a file initialized with rcs -i */
  626.         if ((newdnumlength==0)&&(Dbranch!=nil)) {
  627.             bufscpy(&newdelnum, Dbranch);
  628.             newdnumlength = countnumflds(Dbranch);
  629.         }
  630.         if (newdnumlength==0) bufscpy(&newdelnum, "1.1");
  631.         else if (newdnumlength==1) bufscat(&newdelnum, ".1");
  632.         else if (newdnumlength>2) {
  633.             error("Branch point doesn't exist for %s.",newdelnum.string);
  634.                     return false;
  635.                 } /* newdnumlength == 2 is OK;  */
  636.                 olddeltanum=nil;
  637.                 Head = &newdelta;
  638.                 newdelta.next=nil;
  639.                 return true;
  640.         }
  641.         if (newdnumlength==0) {
  642.                 /* derive new revision number from locks */
  643.         switch (findlock(true, &targetdelta)) {
  644.  
  645.           default:
  646.             /* found two or more old locks */
  647.             return false;
  648.  
  649.           case 1:
  650.                     /* found an old lock */
  651.                     olddeltanum=targetdelta->num;
  652.                     /* check whether locked revision exists */
  653.             if (!genrevs(olddeltanum,(char*)nil,(char*)nil,(char*)nil,&gendeltas)) return false;
  654.                     if (targetdelta==Head) {
  655.                         /* make new head */
  656.                         newdelta.next=Head;
  657.                         Head= &newdelta;
  658.             incnum(olddeltanum, &newdelnum);
  659.             } else if (!targetdelta->next && countnumflds(olddeltanum)>2) {
  660.                         /* new tip revision on side branch */
  661.                         targetdelta->next= &newdelta;
  662.                         newdelta.next = nil;
  663.             incnum(olddeltanum, &newdelnum);
  664.                     } else {
  665.                         /* middle revision; start a new branch */
  666.             bufscpy(&newdelnum, "");
  667.             if (!addbranch(targetdelta,&newdelnum)) return false;
  668.                     }
  669.             return true; /* successful use of existing lock */
  670.  
  671.           case 0:
  672.                     /* no existing lock; try Dbranch */
  673.                     /* update newdelnum */
  674.             if (StrictLocks || !myself(RCSstat.st_uid)) {
  675.             error("no lock set by %s",getcaller());
  676.                         return false;
  677.                     }
  678.                     if (Dbranch) {
  679.             bufscpy(&newdelnum, Dbranch);
  680.                     } else {
  681.             incnum(Head->num, &newdelnum);
  682.                     }
  683.             newdnumlength = countnumflds(newdelnum.string);
  684.                     /* now fall into next statement */
  685.                 }
  686.         }
  687.         if (newdnumlength<=2) {
  688.                 /* add new head per given number */
  689.                 olddeltanum=Head->num;
  690.                 if(newdnumlength==1) {
  691.                     /* make a two-field number out of it*/
  692.             if (cmpnumfld(newdelnum.string,olddeltanum,1)==0)
  693.             incnum(olddeltanum, &newdelnum);
  694.             else
  695.             bufscat(&newdelnum, ".1");
  696.                 }
  697.         if (cmpnum(newdelnum.string,olddeltanum) <= 0) {
  698.                     error("deltanumber %s too low; must be higher than %s",
  699.               newdelnum.string, Head->num);
  700.                     return false;
  701.                 }
  702.         if (!(targetdelta=removelock(Head))) return false;
  703.         if (!genrevs(olddeltanum,(char*)nil,(char*)nil,(char*)nil,&gendeltas)) return false;
  704.                 newdelta.next=Head;
  705.                 Head= &newdelta;
  706.         } else {
  707.                 /* put new revision on side branch */
  708.                 /*first, get branch point */
  709.         tp = newdelnum.string;
  710.         for (i = newdnumlength - (newdnumlength&1 ^ 1);  (--i);  )
  711.             while (*tp++ != '.')
  712.                 ;
  713.         *--tp = 0; /* Kill final dot to get old delta temporarily. */
  714.         if (!(targetdelta=genrevs(newdelnum.string,(char*)nil,(char*)nil,(char*)nil,&gendeltas)))
  715.                      return false;
  716.         olddeltanum = targetdelta->num;
  717.         if (cmpnum(olddeltanum, newdelnum.string) != 0) {
  718.             error("can't find branchpoint %s", newdelnum.string);
  719.                     return false;
  720.                 }
  721.         *tp = '.'; /* Restore final dot. */
  722.         if (!addbranch(targetdelta,&newdelnum)) return false;
  723.         }
  724.         return true;
  725. }
  726.  
  727.  
  728.  
  729.     static int
  730. addbranch(struct hshentry *branchpoint,struct buf *num)
  731. /* adds a new branch and branch delta at branchpoint.
  732.  * If num is the null string, appends the new branch, incrementing
  733.  * the highest branch number (initially 1), and setting the level number to 1.
  734.  * the new delta and branchhead are in globals newdelta and newbranch, resp.
  735.  * the new number is placed into num.
  736.  * returns false on error.
  737.  */
  738. {
  739.     struct branchhead *bhead, **btrail;
  740.     struct buf branchnum;
  741.     int result;
  742.     unsigned field, numlength;
  743.     static struct branchhead newbranch;  /* new branch to be inserted */
  744.  
  745.     numlength = countnumflds(num->string);
  746.  
  747.         if (branchpoint->branches==nil) {
  748.                 /* start first branch */
  749.                 branchpoint->branches = &newbranch;
  750.                 if (numlength==0) {
  751.             bufscpy(num, branchpoint->num);
  752.             bufscat(num, ".1.1");
  753.         } else if (numlength&1)
  754.             bufscat(num, ".1");
  755.                 newbranch.nextbranch=nil;
  756.  
  757.     } else if (numlength==0) {
  758.                 /* append new branch to the end */
  759.                 bhead=branchpoint->branches;
  760.                 while (bhead->nextbranch) bhead=bhead->nextbranch;
  761.                 bhead->nextbranch = &newbranch;
  762.         bufautobegin(&branchnum);
  763.         getbranchno(bhead->hsh->num, &branchnum);
  764.         incnum(branchnum.string, num);
  765.         bufautoend(&branchnum);
  766.         bufscat(num, ".1");
  767.                 newbranch.nextbranch=nil;
  768.         } else {
  769.                 /* place the branch properly */
  770.         field = numlength - (numlength&1 ^ 1);
  771.                 /* field of branch number */
  772.         btrail = &branchpoint->branches;
  773.         while (0 < (result=cmpnumfld(num->string,(*btrail)->hsh->num,field))) {
  774.             btrail = &(*btrail)->nextbranch;
  775.             if (!*btrail) {
  776.                 result = -1;
  777.                 break;
  778.             }
  779.                 }
  780.         if (result < 0) {
  781.                         /* insert/append new branchhead */
  782.             newbranch.nextbranch = *btrail;
  783.             *btrail = &newbranch;
  784.             if (numlength&1) bufscat(num, ".1");
  785.                 } else {
  786.                         /* branch exists; append to end */
  787.             bufautobegin(&branchnum);
  788.             getbranchno(num->string, &branchnum);
  789.             targetdelta=genrevs(branchnum.string,(char*)nil,
  790.                         (char*)nil,(char*)nil,&gendeltas);
  791.             bufautoend(&branchnum);
  792.             if (!targetdelta) return false;
  793.                         olddeltanum=targetdelta->num;
  794.             if (cmpnum(num->string,olddeltanum) <= 0) {
  795.                                 error("deltanumber %s too low; must be higher than %s",
  796.                       num->string,olddeltanum);
  797.                                 return false;
  798.                         }
  799.             if (!removelock(targetdelta)) return false;
  800.             if (numlength&1) incnum(olddeltanum,num);
  801.                         targetdelta->next= &newdelta;
  802.                         newdelta.next=nil;
  803.                         return true; /* Don't do anything to newbranch */
  804.                 }
  805.         }
  806.         newbranch.hsh = &newdelta;
  807.         newdelta.next=nil;
  808.         return true;
  809. }
  810.  
  811.  
  812.  
  813.     static void
  814. incnum(const char *onum,struct buf *nnum)
  815. /* Increment the last field of revision number onum by one and
  816.  * place the result into nnum.
  817.  */
  818. {
  819.     register const char *sp;
  820.     register char *tp;
  821.     register unsigned i;
  822.  
  823.     sp = onum;
  824.     bufalloc(nnum, strlen(sp)+2);
  825.     tp = nnum->string;
  826.     for (i=countnumflds(sp);  (--i);  ) {
  827.         while (*sp != '.') *tp++ = *sp++;
  828.         *tp++ = *sp++;  /* copy dot also */
  829.     }
  830.     VOID sprintf(tp, "%d", atoi(sp)+1);
  831. }
  832.  
  833.  
  834.  
  835.     static struct hshentry *
  836. removelock(struct hshentry * delta)
  837. /* function: Finds the lock held by caller on delta,
  838.  * removes it, and returns a pointer to the delta.
  839.  * Prints an error message and returns nil if there is no such lock.
  840.  * An exception is if !StrictLocks, and caller is the owner of
  841.  * the RCS file. If caller does not have a lock in this case,
  842.  * delta is returned.
  843.  */
  844. {
  845.         register struct lock * next, * trail;
  846.     const char *num;
  847.         struct lock dummy;
  848.         int whomatch, nummatch;
  849.  
  850.         num=delta->num;
  851.         dummy.nextlock=next=Locks;
  852.         trail = &dummy;
  853.         while (next!=nil) {
  854.         whomatch = strcmp(getcaller(), next->login);
  855.                 nummatch=strcmp(num,next->delta->num);
  856.                 if ((whomatch==0) && (nummatch==0)) break;
  857.             /*found a lock on delta by caller*/
  858.                 if ((whomatch!=0)&&(nummatch==0)) {
  859.                     error("revision %s locked by %s",num,next->login);
  860.                     return nil;
  861.                 }
  862.                 trail=next;
  863.                 next=next->nextlock;
  864.         }
  865.         if (next!=nil) {
  866.                 /*found one; delete it */
  867.                 trail->nextlock=next->nextlock;
  868.                 Locks=dummy.nextlock;
  869.                 next->delta->lockedby=nil; /* reset locked-by */
  870.                 return next->delta;
  871.         } else {
  872.         if (StrictLocks || !myself(RCSstat.st_uid)) {
  873.             error("no lock set by %s for revision %s",getcaller(),num);
  874.                     return nil;
  875.                 } else {
  876.                         return delta;
  877.                 }
  878.         }
  879. }
  880.  
  881.  
  882.  
  883.     static const char *
  884. getdate()
  885. /* Return a pointer to the current date.  */
  886. {
  887.     static char buffer[datesize]; /* date buffer */
  888.     time_t t;
  889.  
  890.     if (!buffer[0]) {
  891.         t = time((time_t *)0);
  892.         if (t == -1)
  893.             faterror("time not available");
  894.         time2date(t, buffer);
  895.     }
  896.         return buffer;
  897. }
  898.  
  899.  
  900.     static const char *
  901. xpandfile (const char *unexfname,
  902.            const char *dir,
  903.            const struct hshentry *delta)
  904. /* Function: Reads file unexpfname and copies it to a
  905.  * file in dir, performing keyword substitution with data from delta.
  906.  * returns the name of the expanded file if successful, nil otherwise.
  907.  */
  908. {
  909.     const char *targetfname;
  910.         FILE * unexfile, *exfile;
  911.  
  912.     targetfname = makedirtemp(dir,0);
  913.     errno = 0;
  914.     if (!(unexfile = fopen(unexfname, "r"))) {
  915.         eerror(unexfname);
  916.         return nil;
  917.     }
  918.     errno = 0;
  919.     if (!(exfile = fopen(targetfname, "w"))) {
  920.         eerror(targetfname);
  921.         error("can't expand file %s",unexfname);
  922.         ffclose(unexfile);
  923.                 return nil;
  924.         }
  925.     if (Expand == OLD_EXPAND)
  926.         fastcopy(unexfile,exfile);
  927.     else
  928.         while (0 < expandline(unexfile,exfile,delta,false,(FILE*)nil))
  929.             ;
  930.         ffclose(unexfile);ffclose(exfile);
  931.         return targetfname;
  932. }
  933.  
  934.  
  935.     static int
  936. mustcheckin (const char *unexfname,const struct hshentry *delta)
  937. /* Function: determines whether checkin should proceed.
  938.  * Compares the workfilename with unexfname, disregarding keywords.
  939.  * If the 2 files differ, returns true. If they do not differ, asks the user
  940.  * whether to return true or false (i.e., whether to checkin the file anyway);
  941.  * the default answer is false.
  942.  * Shortcut: If forceciflag is set, mustcheckin() always returns true.
  943.  */
  944. {
  945.     int result;
  946.  
  947.         if (forceciflag) return true;
  948.  
  949.         if (!rcsfcmp(workfilename,unexfname,delta)) return true;
  950.         /* If files are different, must check them in. */
  951.  
  952.         /* files are the same */
  953.     if (!(result = yesorno(false,
  954.         "File %s is unchanged with respect to revision %s\ncheckin anyway? [ny](n): ",
  955.         workfilename, delta->num
  956.     ))) {
  957.         error("%scheckin aborted",
  958.             !quietflag && ttystdin()  ?  ""  :  "file is unchanged; "
  959.         );
  960.         }
  961.         return result;
  962. }
  963.  
  964.  
  965.  
  966.  
  967. /* --------------------- G E T L O G M S G --------------------------------*/
  968.  
  969.  
  970.     static struct cbuf
  971. getlogmsg()
  972. /* Function: obtains a log message and returns a pointer to it.
  973.  * If a log message is given via the -m option, a pointer to that
  974.  * string is returned.
  975.  * If this is the initial revision, a standard log message is returned.
  976.  * Otherwise, reads a character string from the terminal.
  977.  * Stops after reading EOF or a single '.' on a
  978.  * line. getlogmsg prompts the first time it is called for the
  979.  * log message; during all later calls it asks whether the previous
  980.  * log message can be reused.
  981.  * returns a pointer to the character string; the pointer is always non-nil.
  982.  */
  983. {
  984.     static const char
  985.         emptych[] = "*** empty log message ***",
  986.         initialch[] = "Initial revision";
  987.     static const struct cbuf
  988.         emptylog = { emptych, sizeof(emptych)-sizeof(char) },
  989.         initiallog = { initialch, sizeof(initialch)-sizeof(char) };
  990.     static struct buf logbuf;
  991.     static struct cbuf logmsg;
  992.  
  993.     int cin;
  994.     register char *tp;
  995.     register size_t i;
  996.     register const char *p;
  997.     const char *caller, *date;
  998.  
  999.     if (keepflag) {
  1000.         /* generate std. log message */
  1001.         caller = getcaller();
  1002.         p = date = getdate();
  1003.         while (*p++ != '.')
  1004.             ;
  1005.         i = strlen(caller);
  1006.         bufalloc(&logbuf, sizeof(ciklog)+strlen(caller)+4+datesize);
  1007.         tp = logbuf.string;
  1008.         VOID sprintf(tp,
  1009.             "%s%s at %s%.*s/%.2s/%.2s %.2s:%.2s:%s",
  1010.             ciklog, caller,
  1011.             date[2]=='.' && VERSION(5)<=RCSversion  ?  "19"  :  "",
  1012.             p-date-1, date,
  1013.             p, p+3, p+6, p+9, p+12
  1014.         );
  1015.         logmsg.string = tp;
  1016.         logmsg.size = strlen(tp);
  1017.         return logmsg;
  1018.     }
  1019.  
  1020.     if (msg.size) return msg;
  1021.  
  1022.     if (!olddeltanum && (
  1023.         cmpnum(newdelnum.string,"1.1")==0 ||
  1024.         cmpnum(newdelnum.string,"1.0")==0
  1025.     ))
  1026.         return initiallog;
  1027.  
  1028.     if (logmsg.size) {
  1029.                 /*previous log available*/
  1030.         if (yesorno(true, "reuse log message of previous file? [yn](y): "))
  1031.         return logmsg;
  1032.         }
  1033.  
  1034.         /* now read string from stdin */
  1035.     if (feof(stdin))
  1036.         faterror("can't reread redirected stdin for log message; use -m");
  1037.     if (ttystdin())
  1038.         aputs("enter log message:\n(terminate with single '.' or end of file)\n>> ",stderr);
  1039.  
  1040.     i = 0;
  1041.     tp = logbuf.string;
  1042.     while ((cin = getcstdin()) != EOF) {
  1043.         if (cin=='\n') {
  1044.             if (i && tp[i-1]=='.' && (i==1 || tp[i-2]=='\n')) {
  1045.                 /* Remove trailing '.'. */
  1046.                 --i;
  1047.                 break;
  1048.             }
  1049.             if (ttystdin()) aputs(">> ", stderr);
  1050.         }
  1051.         bufrealloc(&logbuf, i+1);
  1052.         tp = logbuf.string;
  1053.         tp[i++] = cin;
  1054.                 /*SDELIM will be changed to double SDELIM by putdtext*/
  1055.         } /* end for */
  1056.  
  1057.         /* now check whether the log message is not empty */
  1058.     logmsg = cleanlogmsg(tp, i);
  1059.     if (logmsg.size)
  1060.         return logmsg;
  1061.     return emptylog;
  1062. }
  1063.  
  1064. /*  Make a linked list of Symbolic names  */
  1065.  
  1066.         static void
  1067. addassoclst(int flag, char *sp)
  1068. {
  1069.         struct Symrev *pt;
  1070.  
  1071.     pt = talloc(struct Symrev);
  1072.     pt->ssymbol = sp;
  1073.     pt->override = flag;
  1074.     pt->nextsym = nil;
  1075.     if (lastassoc)
  1076.             lastassoc->nextsym = pt;
  1077.     else
  1078.             assoclst = pt;
  1079.     lastassoc = pt;
  1080.     return;
  1081. }
  1082.